<?php

namespace App\Customer\Model;

use EchoPHP\Facades\Db;
use EchoPHP\Facades\Log;
use EchoPHP\Facades\Validation;
use App\Web\Model\CaseModel;

class ResumeRecommendDetail extends Model {
    private $tablename = 'ResumeRecommendDetail';
    protected $field = 'id,recommendid,subid,caseid,resumeid,createdate,updatedate,status,memo,creator';

    /**
     * 获取推荐简历总数
     */
    public function getRecommendCount($search) {
        $field = 'count(1) as count';
        return $this->getRecommendList($field, $search)[0]['count'];
    }

    /**
     * 获取推荐简历记录
     */
    public function getRecommendList($field, $search, $rowFrom = 0, $rowTo = 0, $orderBy = '') {
        if (empty($search['caseid'])) {
            return null;
        }

        $field = $field ?: $this->field;
        $sql = 'SELECT ' . $field . ' FROM ' . $this->tablename . " where 1=1 ";
        $var = array();
        $where = '';
        if(is_numeric($search['caseid'])) {
            $where = ' and caseid =? ';
            $var = [$search['caseid']];
        } elseif(is_string($search['caseid'])) {
            $caseIds = array_filter(explode(',', $search['caseid']));
            foreach ($caseIds as $key => $item) {
                if (!is_numeric($item)) {
                    unset($caseIds[$key]);
                }
            }
            $this->arrValueToKey($caseIds);
            $placeTag = $this->arrAppand($var, $caseIds);
            $where .= " and  caseid IN(' . $placeTag . ')";
        }

        if (!empty($search['recommendid'])) {
            $where .= ' and recommendid = ? ';
            $var[] = $search['recommendid'];
        }

        if (!empty($search['status']) && $search['status'] != '00') {
            $where .= ' and status = ? ';
            $var[] = $search['status'];
        }
        $sql .= $where;
        $sql .= empty($orderBy) ? ' ORDER BY updatedate desc,status,id' : (' ORDER BY ' . $orderBy);

        return Db::select($sql, $var, $rowFrom, $rowTo);
    }

    /**
     * 获取推荐简历单条
     */
    public function getRecommendOne($field, $id, $resumeid) {
        if (empty($id) || empty($resumeid)
            || !is_numeric($id) || !is_numeric($resumeid)) {
            return null;
        }

        $field = $field ?: $this->field;
        $sql = 'SELECT ' . $field . ' FROM ' . $this->tablename . ' WHERE id =? and resumeid =? ';
        $var = [$id, $resumeid];
        return Db::selectOne($sql, $var);
    }

    /**
     * 根据caseid分组统计获取本批次推荐的简历数量
     */
    public function getCaseResumeCount($recommendid) {
        if (empty($recommendid) || !is_numeric($recommendid)) {
            return null;
        }

        $sql = 'SELECT caseid, count(*) as num FROM ResumeRecommendDetail WHERE recommendid = ? group by caseid  ';
        $var = [$recommendid];
        return Db::select($sql, $var);
    }

    /**
     * 获取本次推荐的caseid对应的简历量
     */
    public function getCaseAllResumeCount($recommendid, $caseIds) {
        $caseIds = array_filter(explode(',', $caseIds));
        if (empty($recommendid) || empty($caseIds)) {
            return false;
        }

        foreach ($caseIds as $key => $item) {
            if (!is_numeric($item)) {
                unset($caseIds[$key]);
            }
        }

        if (empty($caseIds)) {
            return false;
        }

        $var = array();
        $this->arrValueToKey($caseIds);
        $placeTag = $this->arrAppand($var, $caseIds);
        if ($placeTag == "") {
            return false;
        }

        $sql = 'SELECT caseid,status,count(*) as count FROM ResumeRecommendDetail WHERE caseid IN(' . $placeTag . ') 
                and recommendid=? group by caseid,status';
        $var[] = $recommendid;
        return Db::select($sql, $var);
    }

    /**
     * 通过subid+caseid获取本次推荐的caseid对应的简历量
     */
    public function getCaseAllResumeCountBySubID($subId, $caseIds) {
        if (empty($subId)) {
            return false;
        }

        $caseIds = array_filter(explode(',', $caseIds));
        if (empty($caseIds)) {
            return false;
        }

        $var = array();
        $this->arrValueToKey($caseIds);
        $placeTag = $this->arrAppand($var, $caseIds);
        if ($placeTag == "") {
            return false;
        }

        $sql = 'SELECT caseid,status,count(*) as count FROM ResumeRecommendDetail WHERE caseid IN(' . $placeTag . ') and subid=? group by caseid,status';
        $var[] = $subId;
        return Db::select($sql, $var);
    }

    /**
     * 企业设置简历处理状态
     */
    public function setRecommendDetailStatus($subId, $ids, $status, $memo = '') {


        $ids = array_filter(explode(',', $ids));
        if (empty($ids)) {
            return false;
        }

        foreach ($ids as $key => $item) {
            if (!is_numeric($item)) {
                unset($ids[$key]);
            }
        }

        if (empty($ids)) {
            return false;
        }

        $recommendStatus = $this->RecommendStatus();
        if (!isset($recommendStatus[$status])) {
            return false;
        }

        $var = array();
        $this->arrValueToKey($ids);
        $placeTag = $this->arrAppand($var, $ids);
        if ($placeTag == "") {
            return false;
        }

        $sql = 'SELECT caseid,caseresumeid,creator FROM ResumeRecommendDetail WHERE  id IN(' . $placeTag . ')';
        $detailList = Db::select($sql, $var);
        if (!is_array($detailList) || count($detailList) < 1) {
            return false;
        }

        $sql = 'UPDATE ResumeRecommendDetail set status = ?, memo=?, updatedate = NOW() WHERE id IN(' . $placeTag . ')';
        //需要再$var顶部增加一个参数
        array_unshift($var, $status, $memo);
        $result = Db::update($sql, $var);
        if ($result < 1) {
            return false;
        }

        $caseModel = new CaseModel();
        $statusTag = [];
        $statusTag['02'] = 'ok';
        $statusTag['04'] = 'wait';
        $statusTag['05'] = 'pass';
        $caseResumeIds = [];
        $oprId = 0;
        foreach ($detailList as $item) {
            $oprId = $item['creator'];
            $caseResumeIds[] = $item['caseresumeid'];
        }

        $result = $caseModel->modifyResumeTagByFeed($oprId, $subId, $caseResumeIds, $statusTag[$status], $memo);

        if ($result['status']  != 1) {
            Log::error('设置简历标签出错：[' . $result['message'] . ']', ['ids' => $ids, 'status' => $status, 'memo' => $memo]);
        }
        return true;
    }

    /*
     * 状态类型
     */
    public function RecommendStatus() {
        $recommendStatus = array();
        $recommendStatus['01'] = '待处理';
        $recommendStatus['02'] = '合格';
        $recommendStatus['04'] = '待定';
        $recommendStatus['05'] = '淘汰';
        $recommendStatus['06'] = '删除';
        return $recommendStatus;
    }
}